import { createStoreBindings } from "mobx-miniprogram-bindings"
import { store } from "../../store/store"
import toast from "../../miniprogram_npm/@vant/weapp/toast/toast"

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        showScanCode: false,
        showFixCode: false,
        des: '',
        stationList: [],
        stationid: null,
        controls: []
    },

    // 页面加载时执行
  onLoad: function () {
    var that = this;
    // 创建地图上下文
    this.mapCtx = wx.createMapContext('map');

    // 获取用户当前位置
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;

        // 设置地图中心点为用户当前位置
        that.mapCtx.moveToLocation({
          latitude: latitude,
          longitude: longitude,
        });
      },
      fail: function() {
        wx.showToast({
          title: '定位失败',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },

  // 地图视野变化时触发
  regionchange: function (e) {
    console.log('视野变化', e);
    // 这里可以添加视野变化时的其他逻辑处理
  },

  // 点击标记点时触发
  markertap: function (e) {
    console.log('点击标记', e);
    // 这里可以处理点击标记点的逻辑
  },

    chose(e) {
        var id = e.target.dataset.id
        this.data.stationList.forEach(item => {
            if (item.id === id) {
                item.chose = true
            } else {
                item.chose = false
            }
        })
        this.setData({
            stationList: this.data.stationList,
            stationid: id
        })
    },
    scan() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
            return
        } 
        if (this.data.stationid === null) {
            toast('请先选择充电桩')
            return
        }
        this.setData({
            showScanCode: true
        })
    },
    realOrder() {
        wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'station/getStationById',
            data: {
                id: this.data.stationid
            }
        }).then(res => {
            if (res.data.statu !== '正常') {
                toast(res.data.name + '正在' + res.data.statu + '!请换一个')
            } else {
                wx.p.request({
                    method: 'POST',
                    url: app.globalData.api + 'order/list',
                    data: {
                        userid: wx.getStorageSync('userInfo').userid,
                        statu: '进行中'
                    }
                }).then(res => {
                    if (!res.data || res.data.length !== 0) {
                        toast('你有一个订单正在进行中！请先结束订单')
                    } else {
                        wx.p.request({
                            method: 'POST',
                            url: app.globalData.api + 'order/saveOrUpdate',
                            data: {
                                userid: wx.getStorageSync('userInfo').userid,
                                stationid: this.data.stationid,
                                statu: '进行中'
                            }
                        }).then(res => {
                            if (res.data.success) {
                                toast('操作成功')
                                wx.switchTab({
                                    url: '/pages/order/order',
                                  })
                            } else {
                                toast(res.data.msg)
                            }
                        })
                    }
                })
            }
        })
        this.setData({
            showScanCode: false
        })
    },
    fix() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
            return
        } 
        if (this.data.stationid === null) {
            toast('请先选择充电桩')
            return
        }
        this.setData({
            show: true
        })
    },
    upFix() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
            return
        } 
        var userid = wx.getStorageSync('userInfo').userid
        if (this.data.stationid === null) {
            toast('请先选择充电桩')
            return
        }
        wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'fix/saveOrUpdate',
            data: {
                userid: userid,
                stationid: this.data.stationid,
                statu: '用户报修',
                des: this.data.des
            }
        }).then(res => {
            if (res.data.success) {
                toast('操作成功')
            } else {
                toast(res.data.msg)
            }
        })
    },
    initStationList() {
        wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'station/list',
            data: {
            }
        }).then(res => {
            res.data.forEach(item => {
                item.x = item.position.split(",")[0]
                item.y = item.position.split(",")[1]
                item.chose = false
            });
            this.setData({
                stationList: res.data
            })
        })
    },
    goto(e) {
        wx.navigateTo({
          url: e.target.dataset.url,
        })
      },
    onLoad: function () {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: ['userInfo','userType'],
            actions: ['setUserInfo','setUserType']
        })
    },
    onShow() {
        this.initStationList()
    },
    onUnload: function () {
        this.storeBindings.destroyStoreBindings()
    }
})